home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / SW Demo / Demo Source / EventLoop.c < prev    next >
Encoding:
Text File  |  1994-10-07  |  8.4 KB  |  290 lines  |  [TEXT/MPCC]

  1. // ----------------------------------------------------------------------------------
  2. // EventLoop.c
  3. // ----------------------------------------------------------------------------------
  4. // Main event loop for a standard Macintosh application
  5. //
  6. // This demo is copywrite 1994 by LexTek Internation.  Feel free to use it for
  7. // whatever purpose you wish.  The SpellWright Library may not be copied or 
  8. // distributed, except when linked to your application that adds significant features
  9. // to the code.  ie. you can't take the library, make a new library and sell it.  You
  10. // can, however, use it without royalties in applications or other such projects.
  11. //
  12. // LexTek International
  13. // 2255 N. University Parkway, Suit 15
  14. // Provo, UT 84604
  15. // (801) 375.8332  Phone
  16. // (801) 375.7654  Fax 
  17. //
  18. // ----------------------------------------------------------------------------------
  19. // History:
  20. //    8/26/94        Clark Goble    Original
  21. //    10/1/94        Clark Goble    Finished cleaning up some of the bugs.  Added cursor
  22. //                hiding when a key is typed.  Cleaned up Sys6 support.
  23. //
  24.  
  25. #include "EventLoop.h"
  26. #include "AEHandler.h"
  27. #include "WindowHandler.h"
  28. #include "TEHandler.h"
  29. #include "Globals.h"
  30.  
  31. void DoMenu(register long msel);
  32. void DoKey(register EventRecord *evt);
  33. void DrawClippedGrowIcon(WindowPtr theWindow);
  34. void DoUpdate(register EventRecord *evt);
  35. void DoActivate(register EventRecord *evt);
  36. void DoMulti(register EventRecord *evt);
  37. void DoClick(register EventRecord *evt);
  38. void DoEvent(void);
  39.  
  40. // ----------------------------------------------------------------------------------
  41.  
  42. extern void MenuHandler(short menuNumber,short itemNumber);
  43. extern void DrawImage(GrafPtr graf);
  44.  
  45. // ----------------------------------------------------------------------------------
  46.  
  47. Boolean        gQuit = false;                // Quit the program when true
  48. Boolean        gHaveWNE = false;            // Do we have WaitNextEvent
  49. Boolean        gInBack = false;            // True if we are in the background
  50. Boolean        gSys7 = false;                // Sys 7 available
  51.  
  52.  
  53. // ----------------------------------------------------------------------------------
  54. // DoMenu
  55. // ----------------------------------------------------------------------------------
  56. // Takes the menu selected and gets from it the menu id and the item id of that
  57. // menu.  It then calls the appropriate menu handler.
  58.  
  59. void DoMenu(register long msel)
  60. {
  61.     int theItem;
  62.     int    theMenu;
  63.     
  64.     theItem = LoWord(msel);
  65.     theMenu = HiWord(msel);
  66.     
  67.     MenuHandler(theMenu, theItem);
  68.     
  69.     HiliteMenu(0);
  70.     
  71. } // DoMenu
  72.  
  73. // ----------------------------------------------------------------------------------
  74. // DoKey
  75. // ----------------------------------------------------------------------------------
  76. // Handles a key command, sending it to either a text window or a menu.
  77.  
  78. void DoKey(register EventRecord *evt)
  79. {
  80.     char        keyChar;
  81.     
  82.     keyChar = (char)evt->message & charCodeMask;
  83.     
  84.     if((evt->modifiers & cmdKey) == FALSE) 
  85.     {    // no command key pressed
  86.         
  87.         DoTEKey(gDocWindow, keyChar );
  88.     } else 
  89.     {    // command key pressed
  90.         DoMenu(MenuKey(keyChar));    
  91.         
  92.     } // if/else
  93. } // DoKey
  94.  
  95. // ----------------------------------------------------------------------------------
  96. // DrawClippedGrowIcon
  97. // ----------------------------------------------------------------------------------
  98. // Draws the grow icon, but clips it 'grow icon region.' (Makes it look a little
  99. // better.)
  100.  
  101. void DrawClippedGrowIcon(WindowPtr theWindow)
  102.  
  103. {
  104.     Rect        theClip;
  105.     RgnHandle    oldClip;
  106.     
  107.     oldClip = NewRgn();        // Save the old clipping region
  108.     GetClip(oldClip);
  109.     
  110.     theClip = theWindow->portRect;
  111.     theClip.left = theClip.right - 15;
  112.     theClip.top = theClip.bottom - 15;
  113.  
  114.     ClipRect(&theClip);
  115.     DrawGrowIcon(theWindow);
  116.     
  117.     SetClip(oldClip);        // Restore the old clipping region
  118. } // DrawClippedGrowIcon
  119.  
  120.  
  121. // ----------------------------------------------------------------------------------
  122. // DoUpdate
  123. // ----------------------------------------------------------------------------------
  124. // Handle an update event.  Here we merely clean up the window, call a TE update,
  125. // update the controls and other such things
  126.  
  127. void DoUpdate(register EventRecord *evt)
  128. {
  129.     WindowPtr    updateWindow;
  130.     GrafPtr        savePort;
  131.     TEHandle    theTE;
  132.     
  133.     
  134.     
  135.     
  136.     updateWindow=(WindowPtr)evt->message;    // The window to update
  137.     theTE = GetTE(updateWindow);
  138.     BeginUpdate(updateWindow);    
  139.     
  140.     GetPort(&savePort);                        // Save the port
  141.     SetPort(updateWindow);                
  142.     EraseRect(&updateWindow->portRect);        // Erase the window
  143.     TEUpdate( &(updateWindow->portRect), theTE );
  144.     DrawClippedGrowIcon(updateWindow);        // Draw the grow icon
  145.     DrawControls(updateWindow);                // Draw the window's controls    
  146.     UpdateDocWindow(updateWindow);
  147.     SetPort(savePort);                        // Restore the port
  148.     
  149.     EndUpdate(updateWindow);
  150.     
  151. } // DoUpdate
  152.  
  153.  
  154. // ----------------------------------------------------------------------------------
  155. // DoActivate
  156. // ----------------------------------------------------------------------------------
  157. // Handle an activate event.  It either activates or deactivates the window.
  158.  
  159. void DoActivate(register EventRecord *evt)
  160. {    
  161.     if(evt->modifiers & activeFlag)
  162.         ActivateWindow((WindowPtr )evt->message);
  163.     else
  164.         DeactivateWindow((WindowPtr )evt->message);
  165.         
  166. } // DoActivate
  167.  
  168. // ----------------------------------------------------------------------------------
  169. // DoMulti
  170. // ----------------------------------------------------------------------------------
  171. // Handle multitasking events. These are the suspend and resume events.
  172. // We said in our SIZE resource that we were MultiFinder aware, therefore we have 
  173. // to activate and deactivate windows ourselves under suspend or resume events.
  174.  
  175. void DoMulti(register EventRecord *evt)
  176. {
  177.     // the OS data is in the high byte
  178.     switch ((evt->message >> 24) & 0x00FF) 
  179.     {    case mouseMovedMessage:
  180.             DoIdle();        // mouse moved is also an idle event
  181.             break;
  182.         case suspendResumeMessage:
  183.  
  184.             gInBack = (evt->message & resumeFlag) == 0;
  185.             if (FrontWindow()) {
  186.                 if (gInBack)
  187.                     DeactivateWindow((WindowPtr )FrontWindow());
  188.                 else
  189.                     ActivateWindow((WindowPtr)FrontWindow());
  190.                 DrawClippedGrowIcon(FrontWindow());
  191.             }
  192.             break;
  193.     } // switch
  194.     
  195. } // DoMulti
  196.  
  197. // ----------------------------------------------------------------------------------
  198. // DoClick
  199. // ----------------------------------------------------------------------------------
  200. // Handle a mouse down event.  
  201.  
  202. void DoClick(register EventRecord *evt)
  203. {
  204.     WindowPtr    theWindow;
  205.  
  206.     switch(FindWindow(evt->where, &theWindow)) 
  207.     {
  208.         case inDesk:        break;
  209.         case inMenuBar:        DoMenu(MenuSelect(evt->where));
  210.                             break;
  211.         case inSysWindow:    SystemClick(evt,theWindow);
  212.                             break;
  213.         case inContent:        DoClickInContent(evt,theWindow);
  214.                             break;
  215.         case inDrag:        DoDragWindow(evt,theWindow);
  216.                             break;
  217.         case inGrow:        DoGrowWindow(evt,theWindow);
  218.                             break;
  219.         case inGoAway:        DoCloseWindow(evt,theWindow);
  220.                             break;
  221.         case inZoomIn:        DoZoom(evt,theWindow,inZoomIn);
  222.                             break;
  223.         case inZoomOut:        DoZoom(evt,theWindow,inZoomOut);
  224.                             break;
  225.         default:            break;
  226.         
  227.     } // switch
  228. } // DoClick
  229.  
  230.  
  231. // ----------------------------------------------------------------------------------
  232. // DoEvent
  233. // ----------------------------------------------------------------------------------
  234. // Handle an event.  
  235.  
  236. void DoEvent(void)
  237. {
  238.     EventRecord    theEvent;
  239.     Boolean        eventOccured;    
  240.     
  241.     while (!gQuit)
  242.     {
  243.         // Support old macs here
  244.         if(gHaveWNE)
  245.             eventOccured = WaitNextEvent(everyEvent,&theEvent,10,nil);
  246.         else {
  247.             SystemTask();
  248.             eventOccured = GetNextEvent(everyEvent, &theEvent);
  249.         } // if/else
  250.         
  251.         // if we have a text window call the idle procedure for it
  252.         if (gDocWindow) 
  253.         {    TEIdle( GetTE(gDocWindow));
  254.         } // if
  255.         
  256.         // handle the events we worry about
  257.         
  258.         if(eventOccured) {
  259.             switch(theEvent.what) {
  260.                 case nullEvent:                                        break;
  261.                 case mouseDown:            DoClick(&theEvent);            break;
  262.                 case mouseUp:                                         break;
  263.                 case keyDown:            DoKey(&theEvent);            break;
  264.                 case keyUp:                                             break;
  265.                 case autoKey:            DoKey(&theEvent);            break;
  266.                 case updateEvt:            DoUpdate(&theEvent);        break;
  267.                 case diskEvt:                                         break;
  268.                 case activateEvt:        DoActivate(&theEvent);        break;
  269.                 case networkEvt:                                    break;
  270.                 case driverEvt:                                         break;
  271.                 case app1Evt:                                         break;
  272.                 case app2Evt:                                         break;
  273.                 case app3Evt:                                         break;
  274.                 case osEvt:                DoMulti(&theEvent);            break;
  275.                 case kHighLevelEvent:    DoHighLevel(&theEvent);        break;
  276.                 default:                                            break;
  277.             } // switch
  278.             
  279.         } else 
  280.         {
  281.             if (gDocWindow)
  282.             {    
  283.                 WindowCursor(gDocWindow);
  284.             } // if
  285.         } // if/else
  286.     } // while
  287. } // DoEvent
  288.  
  289.  
  290.